package com.tianpengtech.front.moduleA;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.plugin.shiro.ext.ShiroInterceptor;
import com.tianpengtech.common.util.ImageUtils;
import com.tianpengtech.common.util.JsonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class H5Controller extends Controller {

    public void index() {
        renderTemplate("index.html");
    }

    @Before(ShiroInterceptor.class)
    public void sshirohiro(){
        renderText("shiro test");
    }


    public void upload(){
        try {
            UploadFile file =  getFile("file");
            String path = ImageUtils.uploadToQiNiu(file,null,"test");
            renderText(path);
        } catch (ValidateException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        }
    }


    public void mqtt(){
        renderTemplate("mqtt.html");
    }

    public void logout(){
        Subject subject = SecurityUtils.getSubject() ;
        subject.logout();
        renderText("log out");
    }
    
    
    public void test(){
    	int pageNum = getParaToInt(GlobalConfigConstant.PARAM_NAME_PAGENUM, 1);
    	int pageSize= getParaToInt(GlobalConfigConstant.PARAM_NAME_PAGESIZE,10);
    	String sql = "select * ";
    	String from = "from dn_student order by id desc";
    	renderJson(JsonUtil.printObject(Db.paginate(pageNum, pageSize, sql,from)));
    }
    
    
    public void login(){
    	
    	String userName = getPara("userName", "");
    	String userPass = getPara("userPass", "");
    	System.out.println("userName"+userName+",userPass"+userPass);
    	renderJson(JsonUtil.printSuccess("登录成功"));
    }
}
